//
//  FliterItemStackView.swift
//  Wallpaper
//
//  Created by    on 2023/12/20.
//

import UIKit

class FilterItemStackView: UIView {
    var headTitle:String
    var titles : [String]
    var isVertical : Bool
    var btns : [UIButton] = []
    lazy var stack = {
        let view : UIStackView = UIStackView()
        view.distribution = .fillEqually
        view.alignment = .leading
        view.axis = self.isVertical == true ? NSLayoutConstraint.Axis.vertical : NSLayoutConstraint.Axis.horizontal
        return view
    }()

    init(headTitle:String,titles:[String],isVertical:Bool) {
        self.titles = titles
        self.headTitle = headTitle
        self.isVertical = isVertical
        super.init(frame: .zero)
        self.addSubviews()
    }
    
    func addSubviews() {
        let label = UILabel()
        label.text = self.headTitle
        label.font = .systemFont(ofSize: 17)
        label.textColor = .white
        self.addSubview(label)
        label.snp.makeConstraints { make in
            make.height.equalTo(30)
            make.left.equalTo(10)
            make.right.equalTo(-10)
            make.top.equalTo(0)
        }
        
        let sep : UIView = UIView()
        sep.backgroundColor = sepColor
        self.addSubview(sep)
        sep.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.height.equalTo(1)
            make.top.equalTo(label.snp.bottom)
        }
        self.addSubview(stack)
        stack.snp.makeConstraints { make in
            make.top.equalTo(label.snp.bottom).offset(20)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.bottom.equalTo(-20)
        }
        
        for (index,title) in titles.enumerated() {
            let btn : UIButton = UIButton(type: .custom)
            btn.setTitle(title, for: .normal)
            btn.setImage(UIImage(named: "bigKuang"), for: .normal)
            btn.setImage(UIImage(named: "selKuang"), for: .selected)
            btn.contentHorizontalAlignment = .left
            btn.titleEdgeInsets = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 0)
            btn.isSelected = index == 0
            btn.addTarget(self, action: #selector(click(_:)), for: .touchUpInside)
            btns.append(btn)
            stack.addArrangedSubview(btn)
            btn.snp.makeConstraints { make in
                if isVertical == true {
                    make.width.equalTo(stack.snp.width)
                } else {
                    make.width.equalTo(stack.snp.width).multipliedBy(1.0/Double(titles.count))
                }
                make.height.equalTo(30)
            }
        }
        self.backgroundColor = subColor
        self.layer.cornerRadius = 10
    }
    
    @objc func click(_ clickBtn:UIButton) {
        guard clickBtn.isSelected == false else {
            return
        }
        clickBtn.isSelected = !clickBtn.isSelected
        for btn in btns {
            if btn != clickBtn {
                btn.isSelected = false
            }
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
